package com.wd.buycar.view.activity

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import androidx.activity.viewModels
import androidx.core.app.ActivityCompat
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.wd.buycar.databinding.ActivityOldCarDetailsBinding
import com.wd.buycar.view.adapter.BuyoldcarDetails_ImgAdapter
import com.wd.common.base.BaseActivity
import com.wd.common.viewmodel.MyViewModel
import java.text.SimpleDateFormat

class OldCarDetailsActivity : BaseActivity<ActivityOldCarDetailsBinding, Any?>() {
    val viewmodel: MyViewModel by viewModels()
    private var adapter: BuyoldcarDetails_ImgAdapter? = null
    override fun getViewBinding(): ActivityOldCarDetailsBinding =
        ActivityOldCarDetailsBinding.inflate(layoutInflater)



    override fun initData() {
        val id = this.intent.getIntExtra("id", 0)
        val token = SPUtils.getInstance().getString("token")

        //收藏车辆
        binding.buyOldcarDetailsShoucang.setOnClickListener {
            if (token != "") {
                if (binding.buyOldcarDetailsShoucang.isChecked) {
                    //收藏成功
                    viewmodel.buy_oldcar_detailsshoucang(id)
                    viewmodel.buy_oldcar_detailsshoucangLiveData.observe(this, Observer {
                        ToastUtils.showLong(it.message)
                    })
                } else {
                    //取消收藏
                    viewmodel.buy_oldcar_deteleshoucang(id)
                    viewmodel.buy_oldcar_deteleshoucangLiveData.observe(this, Observer {
                        ToastUtils.showLong(it.message)
                    })
                }
            } else {
                ToastUtils.showLong("请先登录")
            }
        }

        //详情页赋值
        viewmodel.buy_oldcar_details(id)
        viewmodel.buy_oldcar_detailsLiveData.observe(this, Observer {
            val imageUrlList: List<String> = listOf(
                it.result.bgImageUrl,
                it.result.bgImageUrl,
                it.result.bgImageUrl,
                it.result.bgImageUrl,
                it.result.bgImageUrl,
            )
            //车辆实拍
            adapter = this.let { it1 -> BuyoldcarDetails_ImgAdapter(it1, imageUrlList) }
            binding.buyOldcarDetailsBottomimgRecy.layoutManager = GridLayoutManager(this, 2)
            binding.buyOldcarDetailsBottomimgRecy.adapter = adapter
            //名称
            binding.buyOldcarDetailsName.setText(it.result.carStyleName)
            //价格
            binding.buyOldcarDetailsPrice.setText(it.result.sellingPrice.toString() + "万")
            //上牌地
            binding.buyOldcarDetailsText2.setText(it.result.upPlateNumberAddress)
            binding.buyOldcarDetailsText4.setText(it.result.carLocation)
            //行程
            binding.buyOldcarDetailsView2Text2.setText(it.result.traveledDistance.toString() + "万公里")
            //上牌日期日期
            // 将时间对象进行格式化转换
            val dateFormat = SimpleDateFormat("yyyy年MM月")
            val formattedTime: String = dateFormat.format(it.result.upPlateNumberTime)
            binding.buyOldcarDetailsView2Text1.setText(formattedTime)
            //保险到期时间
            val dateFormat1 = SimpleDateFormat("yyyy-MM")
            val formattedTime1: String = dateFormat1.format(it.result.insuranceExpirationTime)
            binding.buyOldcarDetailsText12.setText(formattedTime1)
            binding.buyOldcarDetailsNewprice.setText("新车指导价:" + it.result.guidePrice.toString() + "万")
            //变速箱
            binding.buyOldcarDetailsText8.setText(it.result.speedChangingBox)

            //车况描述
            binding.buyOldcarDetailsMiaoshu.setText(it.result.carDescribe)


            Glide.with(this)
                .load(it.result.bgImageUrl)
                .into(binding.buyOldcarDetailsTopimg)

        })

        //打电话
        binding.buyOldcarDetailsLianxiUsercar1.setOnClickListener {
            val phoneNumber = "15584789383"//替换为你药拨打的手机号码
            val callntent = Intent(Intent.ACTION_CALL, Uri.parse("tel:$phoneNumber"))
            if (ActivityCompat.checkSelfPermission(
                    this,
                    Manifest.permission.CALL_PHONE
                ) == PackageManager.PERMISSION_GRANTED
            ) {
                this.startActivity(callntent)
            } else {
                val REQUEST_CALL_PERMISSION = 0
                if (this is Activity) {
                    val activity = this as Activity
                    ActivityCompat.requestPermissions(
                        activity,
                        arrayOf(Manifest.permission.CALL_PHONE),
                        REQUEST_CALL_PERMISSION
                    )
                    //在这里使用activity对象
                } else {
                    //处理上下文类型不匹配的情况
                }
            }
        }

        //询问底价
        binding.buyOldcarDetailsXunwenprice.setOnClickListener {
            viewmodel.BuyoldcarInquiry(id,"马海钊","18132550983","唐山")
            viewmodel.BuyoldcarInquiryLiveData.observe(this, Observer {
                ToastUtils.showLong(it.message)
            })
        }
    }
}